import Upload from '../../../../utils/uploadFile' 
var app = getApp()
var d = new Date()
var m = d.getMonth() + 1
var d1 = d.getDate()
if (m<10){
  m = '0' + m
}
if (d1<10){
  d1 = '0' + d1
}
var today = d.getFullYear()+'-'+m+'-'+d1
Page({
  data: {
    qrcode: '',
    code: '13081239423',
    copied: false,
    editable: false,
    child: null,
    sexArr: ['男', '女'],
    today,
    // 身高
    heights: [],
    heightStart: 0,
    heightEnd: 250,
    // 体重
    weights: [],
    weightStart: 0,
    weightEnd: 250,
  },
  onLoad(options){
    this.setData({
      heights: this.generateRange(this.data.heightStart,this.data.heightEnd),
      weights: this.generateRange(this.data.weightStart,this.data.weightEnd),
      child: JSON.parse(options.child)
    })
  },
  // 生成range，身高和体重数组
  generateRange(start,end){
    var range = []
    for(var i = start; i<end;i++){
      range.push(i)
    }
    return range
  },
  getPickerKey(e){
    return e.currentTarget.dataset.key
  },
  getPickerValue(e){
    return e.detail.value
  },
  // 统一处理所有的picker组件change事件
  bindPickerChange(e){
    var key = this.getPickerKey(e)
    var value = this.getPickerValue(e)
    var child = this.data.child
    child[key] = value
    this.setData({
      child
    })
  },
  // 保存
  submit(){
    var _this = this
    if (!this.validateName()) return
    var {childUserId,height, weight, nickname, picture, background, birthday, sex} = this.data.child
    var form = {
      childId: childUserId, height, weight, nickname, picture, background, birthday, sex
    }
    app.middleware.requestAjax({
      serAddr: app.serAddrs['updateBabyDetail'],
      data: form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.showToast({
          title: '保存成功',
          success(){
            setTimeout(()=>{
              // 更新上一页的数据
              _this.updatePrevPageData(form)

              wx.navigateBack()
            },1000)
          }
        })
      }
    })
  },
  // 更新上一页的数据
  updatePrevPageData(form){
    var pageData = getCurrentPages()
    var pageLength = pageData.length
    var prevPageData = pageData[pageLength-2]
    var child = prevPageData.data.child

    child.picture = form.picture
    child.height = form.height
    child.background = form.background
    child.nickname = form.nickname
    child.birthday = form.birthday
    child.weight = form.weight
    child.sex = form.sex

    // 上一页
    prevPageData.setData({
      child
    })

    // 宝贝列表
    var babyListPageData = pageData[pageLength-3]
    // 判断是通过宝贝列表进入的详情还是关心列表
    var list = babyListPageData.data.currentPage === 'mine'?babyListPageData.data.babyList:babyListPageData.data.carefulUserList
    var babyListLen = list.length

    for (var i =0;i<babyListLen;i++) {
      var item = list[i]
      if (item.childId===form.childId) {
        item.picture = form.picture
        item.height = form.height
        item.background = form.background
        item.nickname = form.nickname
        item.birthday = form.birthday
        item.weight = form.weight
        item.sex = Number(form.sex)

        // 列表额外修改gender性别
        item.gender = form.sex == 0?'male':'female'
        break
      }
    }
    var listKey = babyListPageData.data.currentPage === 'mine'?'babyList':'carefulUserList'
    babyListPageData.setData({
      [listKey]: list
    })
  },
  // 设置名称
  setName(e){
    var child = this.data.child
    child.nickname = e.detail.value
    this.setData({
      child
    })
    this.validateName()
  },
  validateName(){
    let flag = true
    if (!this.data.child.nickname.trim()) {
      flag = false
      wx.showToast({
        title: '昵称不能为空',
        icon: 'none'
      })
    }
    return flag
  },
  // 设置背景封面
  setCover(){
    this.uploadImage('background')
  },
  // 设置头像
  setAvator(){
    this.uploadImage('picture')
  },
  // 设置头像和背景统一处理
  uploadImage(childKey){
    var that = this
    Upload.uploadImages({
      uploadSuccess(tempFilePaths){
        var child = that.data.child
        child[childKey] = tempFilePaths[0].pic
        that.setData({
          child
        })
      }
    })
  }
})
